using System;
using System.Runtime.Serialization;
using System.Text;
using System.IO;    
using System.Xml.Serialization;
using RomanJendrusz.Amazon.ETL.Infrastructure.Loging.Core;

namespace RomanJendrusz.Amazon.ETL.Infrastructure.Loging.Log4Net
{
    public class Log4NetAdapter : ILog
    {
        private readonly log4net.ILog _log;

        public Log4NetAdapter(log4net.ILog logToAdopt)
        {
            _log = logToAdopt;
        }

        #region ILog Members

        public void Debug(string message)
        {
            _log.Debug(message);
        }

        public void Debug(object objectToBeLogged)
        {
            _log.Debug(serialiazeObject(objectToBeLogged));
        }

        public void Debug(string message, object objectToBeLogged)
        {
            string serializedObject = serialiazeObject(objectToBeLogged);
            string msg = string.Format("{0}{1}{2}", message, Environment.NewLine, serializedObject);
            _log.Debug(msg);
        }


        public void Debug(string message, Exception exceptionToBeLoggged)
        {
            _log.Debug(message, exceptionToBeLoggged);
        }

        public void Warn(string message)
        {
            _log.Warn(message);
        }

        public void Warn(object objectToBeLogged)
        {
            _log.Warn(serialiazeObject(objectToBeLogged));
        }

        public void Warn(string message, object objectToBeLogged)
        {
            string serializedObject = serialiazeObject(objectToBeLogged);
            string msg = string.Format("{0}{1}{2}", message, Environment.NewLine, serializedObject);
            _log.Warn(msg);
        }

        public void Warn(string message, Exception exceptionToBeLoggged)
        {
            _log.Warn(message, exceptionToBeLoggged);
        }

        public void Error(string message)
        {
            _log.Error(message);
        }

        public void Error(object objectToBeLogged)
        {
            _log.Error(serialiazeObject(objectToBeLogged));
        }

        public void Error(string message, object objectToBeLogged)
        {
            string serializedObject = serialiazeObject(objectToBeLogged);
            string msg = string.Format("{0}{1}{2}", message, Environment.NewLine, serializedObject);
            _log.Error(msg);
        }

        public void Error(string message, Exception ex)
        {
            _log.Error(message, ex);
        }

        public Guid ErrorWithIdentifier(string message)
        {
            Guid errorIdentifier = Guid.NewGuid();
            string mergedMessage = string.Format("Error Id - {0} - {1}", errorIdentifier, message);
            _log.Error(mergedMessage);
            return errorIdentifier;
        }

        public Guid ErrorWithIdentifier(object objectToBeLogged)
        {
            Guid errorIdentifier = Guid.NewGuid();
            string mergedMessage = string.Format("Error Id - {0} - {1}", errorIdentifier, serialiazeObject(objectToBeLogged));
            _log.Error(mergedMessage);
            return errorIdentifier;
        }

        public Guid ErrorWithIdentifier(string message, object objectToBeLogged)
        {
            Guid errorIdentifier = Guid.NewGuid();
            string serializedObject = serialiazeObject(objectToBeLogged);
            string msg = string.Format("Error Id - {0} - {1} {2} {3}", errorIdentifier, message, Environment.NewLine, serializedObject);
            _log.Error(msg);
            return errorIdentifier;
        }

        public Guid ErrorWithIdentifier(string message, Exception exceptionToBeLoggged)
        {
            Guid errorIdentifier = Guid.NewGuid();
            string mergedMessage = string.Format("Error Id - {0} - {1}", errorIdentifier, message);
            _log.Error(mergedMessage, exceptionToBeLoggged);
            return errorIdentifier;
        }

        #endregion

        private static string serialiazeObject(object objectToBeLogged)
        {
            return objectToBeLogged.ToString();
        }
    }
}